// 求连续最多的字符和次数

interface IRes {
    char: string,
    length: number
}
// 嵌套循环
function findContinuousChar1(str: string): IRes {
    const res: IRes = {
        char: '',
        length: 0
    }
    const length = str.length
    if (length === 0) return res
    let tempLength = 0
    for (let i = 0; i < length; i++) {
        tempLength = 0
        for (let j = i; j < length; j++) {
            if (str[i] === str[j]) {
                tempLength++
            }
            if (str[i] !== str[j] || j === length - length - 1) {
                if (tempLength > res.length) {
                    res.char = str[i]
                    res.length = tempLength
                }
                if (i < length - 1) {
                    i = j - 1
                }
                break
            }
        }
    }
    return res
}
// 双指针
function findContinuousChar2(str: string): IRes {
    const res: IRes = {
        char: '',
        length: 0
    }
    const length = str.length
    if (length === 0) return res
    let tempLength = 0
    let i = 0
    let j = 0
    for (; i < length; i++) {
        if (str[i] === str[j]) {
            tempLength++
        }
        if (str[i] !== str[j] || i === length - 1) {
            if (tempLength > res.length) {
                res.char = str[j]
                res.length = tempLength
            }
            tempLength = 0
            if (i < length - 1) {
                j = i
                i--
            }
        }
    }
    return res
}
const str = 'aaaarcccuuuuu';
// console.log(findContinuousChar1(str));
console.log(findContinuousChar2(str));

